package me.zhengjie.modules.houseshowscene.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.houseshowscene.domain.HouseShowScene;
import me.zhengjie.modules.houseshowscene.service.HouseShowSceneService;
import me.zhengjie.modules.houseshowscene.service.dto.HouseShowSceneDto;
import me.zhengjie.modules.houseshowscene.service.dto.HouseShowSceneQueryParam;
import me.zhengjie.modules.houseshowscene.service.mapper.HouseShowSceneMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2021-12-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = HouseShowSceneService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HouseShowSceneServiceImpl extends CommonServiceImpl<HouseShowSceneMapper, HouseShowScene> implements HouseShowSceneService {

    // private final RedisUtils redisUtils;
    private final HouseShowSceneMapper houseShowSceneMapper;

    @Override
    public PageInfo<HouseShowSceneDto> queryAll(HouseShowSceneQueryParam query, Pageable pageable) {
        IPage<HouseShowScene> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<HouseShowScene> page = houseShowSceneMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, HouseShowSceneDto.class);
    }

    @Override
    public List<HouseShowSceneDto> queryAll(HouseShowSceneQueryParam query){
        return ConvertUtil.convertList(houseShowSceneMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), HouseShowSceneDto.class);
    }

    @Override
    public HouseShowScene getById(Long id) {
        return houseShowSceneMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public HouseShowSceneDto findById(Long id) {
        return ConvertUtil.convert(getById(id), HouseShowSceneDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HouseShowSceneDto resources) {
        HouseShowScene entity = ConvertUtil.convert(resources, HouseShowScene.class);
        return houseShowSceneMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(HouseShowSceneDto resources){
        HouseShowScene entity = ConvertUtil.convert(resources, HouseShowScene.class);
        int ret = houseShowSceneMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return houseShowSceneMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<HouseShowSceneDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (HouseShowSceneDto houseShowScene : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("展示配置ID", houseShowScene.getHouseShowConfigId());
              map.put("场景名称", houseShowScene.getText());
              map.put("场景缩略图", houseShowScene.getImg());
              map.put("场景详情内容，在场景底部展示，可空", houseShowScene.getDetail());
              map.put("视野水平高度，默认150", houseShowScene.getHfov());
              map.put("X轴（左耳到右耳）旋转（正数往上，负数往下），默认2", houseShowScene.getPitch());
              map.put("y轴（从脚到头）旋转，默认360", houseShowScene.getYaw());
              map.put("场景类型
cubemap 六张图片组合成全景
panorama 一张全景图片", houseShowScene.getType());
              map.put("当场景类型为panorama是必填，存储单张全景图路径", houseShowScene.getPanorama());
              map.put("当场景类型为cubemap时必填，数据类型为json数组，图片顺序为前、右、后、左、上、下，顺序不允许颠倒", houseShowScene.getCubeMap());
              map.put("更新时间", houseShowScene.getUpdateTime());
              map.put("创建时间", houseShowScene.getCreateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
